// packageStore/pages/purchase/addaccount/index.js
import { api,util, dialog,forms} from '../../../../js/util';
import {router} from '../../../../js/router';
import {finance} from '../../../../js/modules/store';
import authorize from '../../../../js/modules/authorize';
import {area} from "../../../../js/modules/gloab";
import BasePage from '../../../../js/common/base-page'
import {upload} from '../../../../js/modules/material.js'
var app = getApp();
BasePage({
  pageKey:"financeAccountList",
  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    isIphoneX:app.globalData.isIphoneX,
    mainColor:api.titleColor||"#e56b14",//主题色
    accountType:['银行卡','微信','支付宝'],//['请选择','银行账户','资金账户'],
    accountTypeObj: [
      { value: "1", name: "银行卡" },
      { value: "6", name: "微信" },
      { value: "7", name: "支付宝" }
    ],
    accountTypeIndex: 0,
    accountProperty:['请选择','对私','对公'],
    accountPropertyObj: [
      { value: "", name: "请选择" },
      { value: "0", name: "对私" },
      { value: "1", name: "对公" }],
    accountPropertyIndex: 0,
    currency:['请选择','人民币','港元','美元'],
    currencyObj: [
      { value: "", name: "请选择" },
      { value: "CNY", name: "人民币" },
      { value: "HKD", name: "港元" },
      { value: "USD", name: "美元" }],
    currencyIndex: 0,
    bankCode: [],
    bankCodeObj:[],
    bankCodeIndex: 0,
    bankType: ['银行卡'],
    bankTypeObj: [{ value: "00", name: "银行卡" }],
    bankTypeIndex:0,
    showBankInfo:true,
    region: [],
    tree: [],
    multiArray: [[], []],
    multiIndex: [0, 0],
    multiArrayObject: [[], []],
    forms: {
      AccountType: {
        value: 1,
        validate: "isnull",
        error: "请选择帐户类型"
      },
      Name: {
        value: "",
        validate: "isnull|address",
        error: "请输入姓名"
      },
      Account: {
        value: "",
        validate: "isnull|address",
        error: "请输入帐号"
      },
      AccountProperty: {
        value: "",
        validate: "",
        error: "请选择账户属性"
      },
      Currency: {
        value: "",
        validate: "",
        error: "请选择货币类型"
      },
      BankName: {
        value: "",
        validate: "isnull",
        error: "请选择银行"
      },
      BankCode: {
        value: "",
        validate: "isnull",
        error: "请选择银行"
      },
      BankType: {
        value: "00",
        validate: "",
        error: "请选择帐号类型"
      },
      Province: {
        value: "",
        validate: "",
        error: "请选择开户银行所属省"
      },
      City: {
        value: "",
        validate: "",
        error: "请选择开户银行所属市"
      },
      Subbranch: {
        value: "",
        validate: "address",
        error: "请输入支行名称"
      },
      ImgSrc:{
        value: "",
        validate: "",
        error: "请选择照片"
      },
      Status: {
        value: 1
      },
      IsDefault: {
        value: 0
      },
    },
    types:0,//类型  1 分销   2预存款
    ImgSrc:'',//图片
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,'=====types');
    this.setData({
      types:Number(options.id)
    })
    this.getBankAccounts();
    this.load_area();
    if(options.id){
      this.getEditItem()
      wx.setNavigationBarTitle({
        title: '编辑账户',
      })
    }
  },
  getEditItem(){
    var that=this;
    finance.getAccountDetail({id:that.data.types},function(content){
      
      var info = content[0];
      that.data.forms.AccountType.value = info.AccountType;//账户类型的值
      that.data.forms.Account.value = info.Account;//账号的值
      that.data.forms.Name.value = info.Name;//名字
      that.data.forms.Name.validate = info.AccountType == 1?'isnull':'';
      that.data.forms.Status.value= info.Status;//状态
      that.data.forms.IsDefault.value= info.IsDefault;//默认
      that.data.forms.ImgSrc.value= info.ImgSrc =="undefined"?'':info.ImgSrc|| '';//图片

      that.data.forms.BankName.value = info.BankName || '';//银行名称
      that.data.forms.BankCode.value = info.BankCode || '';//银行编号
      that.data.forms.BankName.validate = info.AccountType == 1?"isnull":'';
      that.data.forms.BankCode.validate = info.AccountType == 1?"isnull":'';
      that.data.forms.AccountProperty.value = info.AccountProperty || '';//账户属性
      that.data.forms.Currency.value = info.Currency || '';//货币类型
      that.data.forms.Province.value = info.Province=='undefined'?'': info.Province || '';//省
      that.data.forms.City.value = info.City=='undefined'?'':info.City || '';//市
      that.data.forms.Subbranch.value = info.Subbranch || '';//支行分支

      //银行名称
      var bank =that.data.bankCode;
      bank.map((item,index)=>{
        if(item == info.BankName){
          that.data.bankCodeIndex = index;
        }
      })
      that.setData({
        accountTypeIndex:info.AccountType == 1?0:info.AccountType == 6?1:2,//账户类型索引
        showBankInfo:info.AccountType == 1,//账户类型选择所需要展示的字段
        forms:that.data.forms,
        ImgSrc:info.ImgSrc =="undefined"?'':info.ImgSrc|| '',//图片
        bankCodeIndex:that.data.bankCodeIndex,//银行索引
        accountPropertyIndex:Number(info.AccountProperty)==1?2:Number(info.AccountProperty)==0?1:0,//账户属性
        currencyIndex:info.Currency == 'RMB'|| info.Currency == 'CNY'?1:info.Currency == 'USD'?3:info.Currency == 'HKD'?2:0,//货币类型
        region:[info.Province=='undefined'?'': info.Province || '',info.City=='undefined'?'':info.City || '']
      })
      // console.log(content,'--------56');
    });
  },
  /*
  *获得银行帐号 
  */
  getBankAccounts() {
    var that=this;
    finance.getBankAccounts(function(content){
      var bankCodeObj = [{ value: "", name: "请选择" }];
      var bankCode = ["请选择"];
      content.map((item)=>{
        bankCode.push(item.BankName);
        bankCodeObj.push(item);
      });
      that.setData({
        bankCode: bankCode,
        bankCodeObj: bankCodeObj
      })
    });
  },
  /**
    * 加载地区
    */
  load_area: function () {
    var that = this;
    area.load(function (res) {
      that.setData({
        tree: res.Content
      });
      that.set_default_area();
    });
  },
  /**
   * 设置默认选中项
   */
  set_default_area: function (select_area) {
    var that = this;
    select_area = select_area || ["北京", "北京市辖", "北京市辖"];
    // console.log(select_area);
    var all = that.data.tree;
    // 循环所有的省
    for (var p in all) {
      if (p == select_area[0]) {
        var pcs = all[p];
        // 找到对应的市进行赋值
        for (var c in pcs) {
          that.data.multiArray[1].push(c);
        }
      }
      that.data.multiArray[0].push(p);
    }
    that.setData({
      multiArray: that.data.multiArray
    });

  },
  /**
   * 点确定
   */
  bindMultiPickerChange: function (e) {
    var that = this;
    this.data.region[0] = that.data.multiArray[0][e.detail.value[0]];
    this.data.region[1] = that.data.multiArray[1][e.detail.value[1]];
    this.setData({
      multiIndex: e.detail.value,
      region: that.data.region
    });
    this.data.forms["Province"].value = that.data.region[0];
    this.data.forms["City"].value = that.data.region[1];
  },
  /**
   * 每一列的滚动事件
   */
  bindMultiPickerColumnChange: function (e) {
    var that = this;
    var column = e.detail.column, row = e.detail.value;
    that.data.multiIndex[column] = row;
    var all = that.data.tree;

    switch (e.detail.column) {
      // 切换省
      case 0:
        var p_name = that.data.multiArray[0][row];
        that.data.multiArray[1] = new Array();
        that.data.region[0] = p_name;

        var cities = all[p_name];
        // 获取当前少下面所有的市
        for (var c in cities) {
          that.data.multiArray[1].push(c);
        }

        that.data.region[1] = that.data.multiArray[1][0];
        that.data.multiIndex[0] = row;
        // 给定选中的位置
        that.data.multiIndex[1] = 0;
        break;
      // 市切换选择
      case 1:
        // 获取前面选中的省
        var p_name = that.data.region[0];
        var c_name = that.data.multiArray[1][row];
        that.data.region[1] = c_name;
        break;
    }
    that.setData({
      region: that.data.region,
      multiArray: that.data.multiArray,
      multiIndex: that.data.multiIndex
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 账户类型改变
   */
  bindAccountTypeChange(e){
    var validate = "isnull", showBankInfo=true;
    if (this.data.accountType[e.detail.value]!='银行卡'){
      showBankInfo=false;
      validate="";
    };
    this.setData({
      accountTypeIndex:e.detail.value,
      showBankInfo: showBankInfo
    });
    this.data.forms.AccountProperty.validate = showBankInfo?'':'';//账户属性
    this.data.forms.Currency.validate = showBankInfo?'':'';//货币类型
    this.data.forms.BankName.validate = validate;
    this.data.forms.BankCode.validate = validate;
    this.data.forms.Name.validate = validate;//姓名
    this.data.forms.BankType.validate = showBankInfo?'':''; //帐号类型
    this.data.forms.BankType.value = showBankInfo?'00':'';
    this.data.forms.Province.validate = showBankInfo?'':'';
    this.data.forms.City.validate = showBankInfo?'':'';
    this.data.forms[e.target.id].value =this.data.accountTypeObj[ e.detail.value].value;
  },
  /**
   * 账户属性改变
   */
  bindPropsChange(e){
    this.setData({
      accountPropertyIndex: e.detail.value
    });
    this.data.forms[e.target.id].value = this.data.accountPropertyObj[e.detail.value].value;
  },
  /**
   * 货币类型改变
   */
  bindCurrencyChange(e){
    this.setData({
      currencyIndex: e.detail.value
    });
    this.data.forms[e.target.id].value = this.data.currencyObj[e.detail.value].value;
  },
  /**
   * 银行名称改变
   */
  bindBankCodeChange(e){
    this.setData({
      bankCodeIndex: e.detail.value
    });
    this.data.forms[e.target.id].value = this.data.bankCodeObj[e.detail.value].Code;
    this.data.forms.BankName.value = this.data.bankCodeObj[e.detail.value].BankName
  },
  /**
   * 账号类型改变
   */
  bindBankTypeChange(e) {
    this.setData({
      bankTypeIndex: e.detail.value
    });
    this.data.forms[e.target.id].value = this.data.bankTypeObj[e.detail.value].value;
  },
  /**
 * 表单值绑定
 */
  bindFormValue: function (e) {

    this.data.forms[e.target.id].value = e.detail.value;
    // console.log(e.detail.value,'=-=-=-=');
  },
  onSwitch(e){
    this.data.forms[e.target.id].value = e.detail.value ? 1 : 0;
  },
  onSubmit(){
    if(this.data.types > 0){
      var forms = this.data.forms;
      finance.bindEditAccount(forms,this.data.types,function(data){
        setTimeout(function(){
          router.navigateBack({
            account: data
          });
        },1000)
      });
    }else{
      finance.bindAddAccount(this.data.forms,function(data){
        setTimeout(function(){
          router.navigateBack({
            account: data
          });
        },1000)
      });
    }
    
  },
  bindUploadSign(){
    var that=this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var data = {num:1011,type:1,tempFilePaths:tempFilePaths};
        upload.upload_image_server(data, function (image_list) {
          if (image_list.length==0){
            util.alert("上传失败");
            return;
          }
          that.data.forms.ImgSrc.value = image_list.join(",");
          that.setData({
            ImgSrc: image_list.join(",")
          });
        })
      }
    });
  },
  // 删除照片
  onDelImageEvent(){
    var that = this;
    that.data.forms.ImgSrc.value ='';
    that.setData({
      ImgSrc: ''
    });
  },
})